package cn.fxl.system.service.impl;

import cn.fxl.system.domain.Menu;
import cn.fxl.system.mapper.MenuMapper;
import cn.fxl.system.service.IMenuService;
import cn.fxl.basic.service.impl.BasicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class MenuServiceImpl extends BasicServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> getMenuTree() {
        List<Menu> menus = menuMapper.getAll();
        HashMap<Long, Menu> longMenuHashMap = new HashMap<>();
        for (Menu menu : menus) {
            longMenuHashMap.put(menu.getId(),menu);
        }
        ArrayList<Menu> menuTree = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParent_id()==null){
                menuTree.add(menu);
            }else {
                Long parent_id = menu.getParent_id();
                Menu parent = longMenuHashMap.get(parent_id);
                List<Menu> children = parent.getChildren();
                children.add(menu);
            }
        }
        return menuTree;
    }
}
